package com.test.burnincash.aaahpoppomelo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.test.burnincash.R;
import com.test.burnincash.aaahpoppomelo.base.BaseActivity_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.OrderItem_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.bean.Order_hpoppomelo;
import com.test.burnincash.aaahpoppomelo.dialog.ShareOrderDialog_hpoppomelo;

import java.util.ArrayList;

public class OrderDetailActivity_hpoppomelo extends BaseActivity_hpoppomelo {

    private TextView orderNumTv;
    private TextView dateTv;
    private TextView customerTv;

    private LinearLayout itemGroupLayout;
    private TextView allSumTv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail_hpoppomelo);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateData();
    }

    private void init(){

        setBackBtn_burnin(findViewById(R.id.activity_order_detail_ib_back));

        orderNumTv = findViewById(R.id.activity_order_detail_tv_order_number);
        dateTv = findViewById(R.id.activity_order_detail_tv_date);
        customerTv = findViewById(R.id.activity_order_detail_tv_customer);
        itemGroupLayout = findViewById(R.id.activity_order_detail_layout_order_item_group);
        allSumTv = findViewById(R.id.activity_order_detail_tv_all_sum);

        findViewById(R.id.activity_order_detail_ib_share).setOnClickListener(v -> new ShareOrderDialog_hpoppomelo(this,Order_hpoppomelo.optionOrder).show());

        findViewById(R.id.activity_order_detail_ib_edit_order).setOnClickListener(v -> {
            Intent intent = new Intent(this, EditOrderActivity_hpoppomelo.class);
            startActivity(intent);
        });

    }

    private void updateData(){

        orderNumTv.setText(String.valueOf(Order_hpoppomelo.optionOrder.getOrderNumber()));
        dateTv.setText(Order_hpoppomelo.optionOrder.getDate());
        customerTv.setText(Order_hpoppomelo.optionOrder.getCustomer());

        updateItems(Order_hpoppomelo.optionOrder);
    }

    private void updateItems(Order_hpoppomelo order){
        itemGroupLayout.removeAllViews();

        ArrayList<OrderItem_hpoppomelo> items = order.getItems();

        int allsum = 0;
        boolean grayBg = true;

        for (OrderItem_hpoppomelo item:items){
            showItem(item,grayBg);
            grayBg = !grayBg;
            allsum += item.getSum();
        }

        allSumTv.setText(String.valueOf(allsum));

    }

    private void showItem(OrderItem_hpoppomelo orderItem,boolean grayBg){
        View itemView = LayoutInflater.from(this).inflate(R.layout.item_order_item_hpoppomelo,null);

        View bgView = itemView.findViewById(R.id.view_order_item_layout_bg);
        TextView noTv = itemView.findViewById(R.id.view_order_item_tv_no);
        TextView itemNameTv = itemView.findViewById(R.id.view_order_item_tv_item_name);
        TextView priceTv = itemView.findViewById(R.id.view_order_item_tv_price);
        TextView quantityTv = itemView.findViewById(R.id.view_order_item_tv_quantity);
        TextView sumTv = itemView.findViewById(R.id.view_order_item_tv_sum);

        if (grayBg) bgView.setBackgroundColor(ContextCompat.getColor(this,R.color.gray_lll));
        noTv.setText(String.valueOf(orderItem.getNo()));
        itemNameTv.setText(orderItem.getItemName());
        priceTv.setText(String.valueOf(orderItem.getPrice()));
        quantityTv.setText(String.valueOf(orderItem.getQuantity()));
        sumTv.setText(String.valueOf(orderItem.getSum()));

        itemView.findViewById(R.id.view_order_item_ib_edit).setOnClickListener(v -> {
            OrderItem_hpoppomelo.optionItem = orderItem;
            Intent intent = new Intent(this, EditItemActivity_hpoppomelo.class);
            startActivity(intent);
        });

        itemGroupLayout.addView(itemView);
    }

}